#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
#include "sys/thread.h"
#include "component/logger.h"
using namespace tdl;

//tdl::console_logger log;
//tdl::size_logger log;
tdl::time_logger log;
//tdl::hour_logger log;

struct uinfo
{
	unsigned int	uin;
	string			cip;
public:
	friend ostream& operator<<(ostream& os, const uinfo& ui)
	{
		os << "uin=" << ui.uin << "|cip=" << ui.cip;
		return os;
	}
};

void foo()
{
	uinfo ui;
	ui.cip = "1.1.1.1";
	int i = 0;
	while(1)
	{
		ui.uin = i;
		if(i % 2 == 0)
			log.info() << ui << "%2=0" << endl;
		else
			log.error() << ui << "%2!=0" << endl;
		++i;
		if(i % 5 == 0)
			log.set_log_level(tdl::LOG_DEBUG);

		usleep(100000);
	}
}

void goo()
{
	uinfo ui;
	ui.cip = "2.2.2.2";
	int i = 10;
	while(1)
	{
		ui.uin = i;
		if(i % 2 == 0)
			log.info() << ui << "%2=0" << endl;
		else
			log.debug("uin=%u|cip=%s|%%2!=0\n", ui.uin, ui.cip.c_str());
//			log.debug() << ui << "%2!=0" << endl;
		++i;
		if(i % 7 == 0)
			log.set_log_level(tdl::LOG_INFO);

		usleep(100000);
	}
}

//////////////////////
int main(int argc, char* argv[])
{
	try {
//		log.initialize();

		tdl::logger_init_info lii;
		lii.log_file_name = "/data/tsdl_logs/logger.log";
//		lii.max_logfile_size = 1000;
		lii.write_block_size = 100;
		lii.roll_interval_min = 10;
		log.initialize(&lii, NULL, tdl::LOG_INFO, '|', true);
	} catch(log_ex& e) {
		cerr << e.what_hell() << endl;
		return 1;
	}

//	log << "illegal call" << endl;

	log.info() << "can't" << "keep" << "original format" << endl;
	log.info(tdl::LOG_ORG_FMT) << "keep " << " original" << " format" << endl;

	thread thr_foo(make_fun_exec(&foo), false);
	thread thr_goo(make_fun_exec(&goo), false);

	thr_foo.run();
	thr_goo.run();

	thr_foo.wait();
	thr_goo.wait();

	return 0;
}

